#include <string>
using namespace std;
/*
 * @lc app=leetcode.cn id=38 lang=cpp
 *
 * [38] 外观数列
 */

// @lc code=start
class Solution {
public:
    string countAndSay(int n) {
        string init = "1";
        for (int i = 1; i < n; i++) {
            string t; int j = 0, count = 1;
            while (j < init.size()) {
                char x = init[j];
                while (j + 1 < init.size() && init[j] == init[j + 1]) {
                    count++; j++;
                }
                t.push_back(count + '0');
                t.push_back(x);
                count = 1; j++;
            }
            init = t;
        }
        return init;
    }
};
// @lc code=end

